//
//  NewZhangHaoAnQuanViewController.m
//  wangzhe
//
//  Created by 李 佳伟 on 2018/3/30.
//  Copyright © 2018年 李 佳伟. All rights reserved.
//

#import "NewZhangHaoAnQuanViewController.h"
#import "JTCSM.h"
#import "MyInfoView.h"
#import "NewChangepasswordViewController.h"
static CGFloat myinfoHeight = 50;
@interface NewZhangHaoAnQuanViewController ()<UIAlertViewDelegate>
{
    UISwitch *zhiwenSwitch;
    UISwitch *shoushiSwitch;
}
@end

@implementation NewZhangHaoAnQuanViewController

- (void)gotBack{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = NO;
    [self.view setBackgroundColor:RGBACOLOR(241, 241, 241, 1)];
    [df isRemoveTarbar:YES];
    leftBarButtonItems_rightBarButtonItems *leftbtn = KCUSTOMNAVIGSTION_ALLOC;
    self.navigationItem.leftBarButtonItem = [leftbtn setBarButtonItemsImageName:@"fanhui.png" frame:KLeftBarButtonItemFrame_image target:self SEL:@selector(gotBack)];
    [self layoutView];
}

- (void)gotoChangePassWord{
    NewChangepasswordViewController *vc = [[NewChangepasswordViewController alloc]init];
    vc.nameTitle = @"登录密码";
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)layoutView{
    
    MyInfoView *headerView = [[MyInfoView alloc]initWithFrame:CGRectMake(0, KagainChangeHeight, KCONTROLLERWith, myinfoHeight) imagename:nil title:@"登录密码" placeholder:nil isShowLabel:ImageViewType radioBtnIndex:0 GroupId:@""];
    [self.view addSubview:headerView];
    headerView.lineView.hidden = YES;
    headerView.userInteractionEnabled = YES;
    [df addTapGestureRecognizerWithTarget:self action:@selector(gotoChangePassWord) andaddView:headerView];
    
    [df newlineViewWithHeightofTop:KVIEW_Y(headerView)+10 superView:self.view spacing:0 color:RGBACOLOR(241, 241, 241, 1) lineHeight:10];
    
    MyInfoView *shoushiView = [[MyInfoView alloc]initWithFrame:CGRectMake(0, KVIEW_Y(headerView)+10, KCONTROLLERWith, myinfoHeight) imagename:nil title:@"手势密码" placeholder:nil isShowLabel:labelType radioBtnIndex:0 GroupId:@""];
    [self.view addSubview:shoushiView];
    shoushiSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(KCONTROLLERWith-90, 5,0, 0)];
    [shoushiView addSubview:shoushiSwitch];
    if ( [KUSERDEFAULTGET(KZhangHuAnQuanKey) isEqualToString:KIsShoushi]){
        [shoushiSwitch setOn:YES animated:YES];
    }else{
        [shoushiSwitch setOn:NO animated:YES];
    }
    //缩小或者放大switch的size
    shoushiSwitch.transform = CGAffineTransformMakeScale(1, 1);
    shoushiSwitch.layer.anchorPoint = CGPointMake(0, 0.3);
    [shoushiSwitch addTarget:self action:@selector(shoushiswitchAction:) forControlEvents:UIControlEventValueChanged];
    
    
    MyInfoView *zhiwenView = [[MyInfoView alloc]initWithFrame:CGRectMake(0, KVIEW_Y(shoushiView), KCONTROLLERWith, myinfoHeight) imagename:nil title:@"指纹密码" placeholder:nil isShowLabel:labelType radioBtnIndex:0 GroupId:@""];
    [self.view addSubview:zhiwenView];
    zhiwenView.lineView.hidden = YES;
    zhiwenSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(KCONTROLLERWith-90, 5,0, 0)];
    [zhiwenView addSubview:zhiwenSwitch];
    if ( [KUSERDEFAULTGET(KZhangHuAnQuanKey) isEqualToString:KIsZhiWen]){
        [zhiwenSwitch setOn:YES animated:YES];
    }else{
        [zhiwenSwitch setOn:NO animated:YES];
    }
    //缩小或者放大switch的size
    zhiwenSwitch.transform = CGAffineTransformMakeScale(1, 1);
    zhiwenSwitch.layer.anchorPoint = CGPointMake(0, 0.3);
    [zhiwenSwitch addTarget:self action:@selector(zhiwenswitchAction:) forControlEvents:UIControlEventValueChanged];
}
-(void)zhiwenswitchAction:(UISwitch *)paramSender{
    
    if ( [KUSERDEFAULTGET(KZhangHuAnQuanKey) isEqualToString:KIsZhiWen]) {
       [df showAlertWithTitle:@"确定取消指纹认证" message:nil btn1:@"确定" btn2:@"取消" btn3:nil delegate:self tag:100];
    }else{
        __weak UISwitch *myshoushiswitch = shoushiSwitch;
        __weak UISwitch *myswitch = zhiwenSwitch;
        [YWFingerprintVerification fingerprintVerificationCallBack:^(NSError *error) {
            if(!error){
                NSLog(@"指纹验证通过");
                [myshoushiswitch setOn:NO animated:true];
                [df showAlertWithTitle:@"指纹认证成功" message:nil btn1:@"知道了" btn2:nil btn3:nil delegate:nil tag:100];
                KUSERDEFAULTSET(KIsZhiWen, KZhangHuAnQuanKey);
            }else{
                NSLog(@"指纹验证失败->%@", @(error.code));
                KUSERDEFAULTSET(@"", KZhangHuAnQuanKey);
                [myswitch setOn:NO animated:true];
            }
        }];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == KAlertVTag+100) {
        if (buttonIndex == 0) {
            KUSERDEFAULTSET(@"", KZhangHuAnQuanKey);
        }
    }
}

-(void)shoushiswitchAction:(UISwitch *)paramSender{
    
    if ( [KUSERDEFAULTGET(KZhangHuAnQuanKey) isEqualToString:KIsShoushi]) {
        [df showAlertWithTitle:@"确定取消手势认证" message:nil btn1:@"确定" btn2:@"取消" btn3:nil delegate:self tag:100];
    }else{
        //创建手势密码
        __weak UISwitch *myswitch = zhiwenSwitch;
        __weak UISwitch *myshoushiswitch = shoushiSwitch;
        [YWUnlockView showUnlockViewWithType:YWUnlockViewCreate superView:self.view callBack:^(BOOL result) {
            NSLog(@"-->%@",@(result));
            if(result){
                NSLog(@"指纹验证通过");
                 [myswitch setOn:NO animated:true];
                [df showAlertWithTitle:@"手势认证成功" message:nil btn1:@"知道了" btn2:nil btn3:nil delegate:nil tag:0];
                KUSERDEFAULTSET(KIsShoushi, KZhangHuAnQuanKey);
            }else{
                KUSERDEFAULTSET(@"", KZhangHuAnQuanKey);
                [myshoushiswitch setOn:NO animated:true];
            }
        }];
    }
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
